package com.wyf.android.advance.rvvp

import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.wyf.android.advance.R
import com.wyf.android.baselib.util.inflate

class RVVPAdapter(
    private val fm: FragmentManager,
    private val onTabChangeListener: (idx: Int) -> Unit
) :
    ListAdapter<RVVPViewData, RVVPAdapter.RVVPViewHolder>(RVVPDiffUtil()) {

    open inner class RVVPViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        open fun bind() {

        }
    }

    inner class TopViewHolder(v: View) : RVVPViewHolder(v) {
        override fun bind() {
            val topViewData = getItem(adapterPosition) as TopViewData
            (itemView as TextView).text = topViewData.t
        }
    }

    inner class TabViewHolder(v: View) : RVVPViewHolder(v) {
        init {
            (itemView as RadioGroup).apply {
                setOnCheckedChangeListener { group, checkedId ->
                    val idx = group.indexOfChild(group.findViewById(checkedId))
                    onTabChangeListener(idx)
                }
            }
        }

        override fun bind() {
            val tabViewData = getItem(adapterPosition) as TabViewData
            (itemView as RadioGroup).apply {
                (getChildAt(tabViewData.idx) as RadioButton).apply {
                    if (!isChecked) {
                        isChecked = true
                    }
                }
            }
        }
    }

    inner class BottomViewHolder(v: View) : RVVPViewHolder(v) {
        init {
            (itemView as ViewPager).apply {
                adapter = RvvpViewPagerAdapter(fm)
                addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                    override fun onPageScrolled(
                        position: Int,
                        positionOffset: Float,
                        positionOffsetPixels: Int
                    ) {
                    }

                    override fun onPageSelected(position: Int) {
                        onTabChangeListener(position)
                    }

                    override fun onPageScrollStateChanged(state: Int) {}
                })
            }
        }

        override fun bind() {
            val bottomViewData = getItem(adapterPosition) as BottomViewData

            (itemView as ViewPager).apply {
                if (currentItem != bottomViewData.idx) {
                    setCurrentItem(bottomViewData.idx, true)
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVVPViewHolder {
        return when (viewType) {
            0 -> TopViewHolder(parent.inflate(R.layout.item_rvvp_top))
            1 -> TabViewHolder(parent.inflate(R.layout.item_rvvp_tab))
            else -> BottomViewHolder(parent.inflate(R.layout.item_rvvp_bottom))
        }
    }

    override fun onBindViewHolder(holder: RVVPViewHolder, position: Int) {
        holder.bind()
    }

    override fun getItemViewType(position: Int): Int {
        return getItem(position).type
    }
}

class RVVPDiffUtil : DiffUtil.ItemCallback<RVVPViewData>() {
    override fun areItemsTheSame(oldItem: RVVPViewData, newItem: RVVPViewData): Boolean {
        return if (oldItem is TopViewData && newItem is TopViewData) {
            oldItem == newItem
        } else {
            true
        }
    }

    override fun areContentsTheSame(oldItem: RVVPViewData, newItem: RVVPViewData): Boolean {
        val r = oldItem == newItem
        return r
    }
}